package com.lanou.myapp.news.funnynews;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.myapp.R;
import com.lanou.myapp.imageloader.GsonRequest;
import com.lanou.myapp.imageloader.VolleySingleton;
import com.lanou.myapp.news.funnynews.contentfunnynews.ContentFunnyNewsActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/10.
 */
public class FunnyNewFragment extends Fragment{

    private Context context;
    private List<TodayInfo> infos;
    private ListView mListView;
    private FunnyNewsAdapter mAdapter;
    private String url = "http://dailyapi.ibaozou.com/api/v1/section/57";
    private RequestQueue queue;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_funny_news,null);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mListView = (ListView) getActivity().findViewById(R.id.listView_funny_news);
        infos = new ArrayList<>();
        queue = Volley.newRequestQueue(getActivity());
        mAdapter = new FunnyNewsAdapter(getActivity());
        GsonRequest<TodayBeans> todayBeansGsonRequest = new GsonRequest<TodayBeans>(url, TodayBeans.class, new Response.Listener<TodayBeans>() {
            @Override
            public void onResponse(final TodayBeans response) {
                mAdapter.addData(response.getArticles());
                mListView.setAdapter(mAdapter);
                mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


                        Intent intent = new Intent(getActivity(), ContentFunnyNewsActivity.class);
                        intent.putExtra("ID", response.getArticles().get(i).getShare_url());
                        startActivityForResult(intent,200);
                    }
                });
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        VolleySingleton.getInstance()._addRequest(todayBeansGsonRequest);


//        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
//            @Override
//            public void onResponse(String response) {
//                Gson gson = new Gson();
//                TodayBeans beans = new TodayBeans();
//                beans = gson.fromJson(response,TodayBeans.class);
//                for (int i = 0; i < beans.getArticles().size(); i++) {
//                    infos.add(beans.getArticles().get(i));
//                }
//                mAdapter = new FunnyNewsAdapter(getActivity(),infos);
//                mListView.setAdapter(mAdapter);
//            }
//        }, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {
//
//            }
//        });
//        queue.add(request);


    }


}
